//
//  dkHUD.m
//  TiaoWei
//
//  Created by dukai on 15/3/17.
//  Copyright (c) 2015年 longcai. All rights reserved.
//

#import "dkHUD.h"
#import "SCGIFImageView.h"
#import "AppDelegate.h"
static dkHUD *HUD =nil;
@implementation dkHUD
{
    UIView *blackView;
    UIWindow *mainWindow;
    SCGIFImageView* gifImageView;
}
+(dkHUD *)share{
    if (HUD == nil) {
        HUD = [[dkHUD alloc]init];
        [HUD initView];
    }
    return HUD;
}
-(void)initView{
    
    blackView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
	[blackView setBackgroundColor:[UIColor clearColor]];
    blackView.userInteractionEnabled = NO;
    
    
    NSString* filePath = [[NSBundle mainBundle] pathForResource:@"3.gif" ofType:nil];
    gifImageView = [[SCGIFImageView alloc] initWithGIFFile:filePath];
    gifImageView.frame = CGRectMake((FULL_SCREEN_WIDTH-50)/2, (FULL_SCREEN_HEIGHT-50)/2, 50, 50);
    gifImageView.center = blackView.center;
	
	UIView *rectView = [[UIView alloc]initWithFrame:gifImageView.frame];
	rectView.backgroundColor = [UIColor blackColor];
	rectView.alpha = 0.5;
    
//    UIApplication* app = [UIApplication sharedApplication];
//    AppDelegate* delegate =  (AppDelegate*)app.delegate;
//    mainWindow = delegate.window;
//    [delegate.window addSubview:blackView];
//    [delegate.window addSubview:gifImageView];
    
    NSEnumerator *frontToBackWindows = [UIApplication.sharedApplication.windows reverseObjectEnumerator];
    for (UIWindow *window in frontToBackWindows){
        
        BOOL windowOnMainScreen = window.screen == [UIScreen mainScreen];
        BOOL windowIsVisible = !window.hidden && window.alpha > 0;
        BOOL windowLevelNormal = window.windowLevel == UIWindowLevelNormal;
        
        if (windowOnMainScreen && windowIsVisible && windowLevelNormal) {
            [window addSubview:blackView];
			//[window addSubview:rectView];
            [window addSubview:gifImageView];
            window.userInteractionEnabled =NO;
            
            mainWindow = window;
            break;
        }
    }

}
-(void)hidden{
    mainWindow.userInteractionEnabled =YES;
    blackView.hidden = YES;
    gifImageView.hidden = YES;
    
}

-(void)show{
    mainWindow.userInteractionEnabled =NO;
    blackView.hidden = NO;
    gifImageView.hidden = NO;

}

-(void)zhushi
{
    mainWindow = nil;
    blackView = nil;
    gifImageView = nil;
    HUD = nil;
}

@end
